using System;
using System.Collections.Generic;
using System.Text;
using IGGCore;
using System.Drawing;
using System.Windows.Forms;
using System.IO;
using System.Xml;
using IGGWindowApp.Data;

namespace IGGWindowApp
{
    /// <summary>
    /// The stadium parser for the window app.
    /// Its add image and positions to the core stadium object.
    /// </summary>
    public class StadiumParser : BaseParser
    {
        /// <summary>
        /// Create a window stadium around a core stadium data object
        /// It assigns a stadium image and sets players positions
        /// </summary>
        public WindowStadium GetStadium(Stadium stadium)
        {
            WindowStadium winStadium = new WindowStadium();
            winStadium.Name = stadium.Name;
            winStadium.VenueCode = stadium.VenueCode;

            string path = "D:\\Parks";

            // DAY STADIUM
            if (File.Exists(path + "\\" + stadium.Name + "_day.jpg"))
            {
                winStadium.StadiumImageDay = new Bitmap(path + "\\" + stadium.Name + "_day.jpg");
            }
            else
            {
                winStadium.StadiumImageDay = new Bitmap(path + "\\unknown.jpg");
            }

            // DUSK STADIUM
            if (File.Exists(path + "\\" + stadium.Name + "_dusk.jpg"))
            {
                winStadium.StadiumImageDusk = new Bitmap(path + "\\" + stadium.Name + "_dusk.jpg");
            }
            else
            {
                winStadium.StadiumImageDusk = winStadium.StadiumImageDay;
            }

            // NIGHT STADIUM
            if (File.Exists(path + "\\" + stadium.Name + "_night.jpg"))
            {
                winStadium.StadiumImageNight = new Bitmap(path + "\\" + stadium.Name + "_night.jpg");
            }
            else
            {
                if (File.Exists(path + "\\" + stadium.Name + "_dusk.jpg"))
                {
                     winStadium.StadiumImageNight = winStadium.StadiumImageDusk;
                }
                else
                {
                    winStadium.StadiumImageNight = winStadium.StadiumImageDay;
                }
            }

            // EMPTY STADIUM
            if (File.Exists(path + "\\" + stadium.Name + "_empty.jpg"))
            {
                winStadium.StadiumImageEmpty = new Bitmap(path + "\\" + stadium.Name + "_empty.jpg");
            }
            else
            {
                winStadium.StadiumImageEmpty = winStadium.StadiumImageDay;
            }

            // RAIN STADIUM
            if (File.Exists(path + "\\" + stadium.Name + "_rain.jpg"))
            {
                winStadium.StadiumImageRain = new Bitmap(path + "\\" + stadium.Name + "_rain.jpg");
            }
            else
            {
                winStadium.StadiumImageRain = winStadium.StadiumImageEmpty;
            }

            // POSITIONS
            XmlDocument stadiumConfigDoc = new XmlDocument();
            try
            {
                if (File.Exists(path + "\\" + stadium.Name + ".xml"))
                {
                    stadiumConfigDoc.Load(path + "\\" + stadium.Name + ".xml");
                }
                else
                {
                    stadiumConfigDoc.Load(path + "\\generic.xml");
                }

                XmlNodeList positionsList = stadiumConfigDoc.GetElementsByTagName("positions");
                
                foreach (XmlNode positionsNode in positionsList)
                {
                    bool daily = (GetAttribute(positionsNode, "time", "day") == "day");
                    bool nightly = (GetAttribute(positionsNode, "time", "day") == "night");
                    XmlNodeList posList = positionsNode.SelectNodes("position");
                    foreach (XmlNode pos in posList)
                    {
                        int index = int.Parse(GetAttribute(pos, "id", "0"));
                        if (daily)
                        {
                            winStadium.StadiumDayPositions[index].X = int.Parse(GetAttribute(pos, "x", "0"));
                            winStadium.StadiumDayPositions[index].Y = int.Parse(GetAttribute(pos, "y", "0"));
                        }
                        else if (nightly)
                        {
                            winStadium.StadiumNightPositions[index].X = int.Parse(GetAttribute(pos, "x", "0")); 
                            winStadium.StadiumNightPositions[index].Y = int.Parse(GetAttribute(pos, "y", "0")); 
                        }
                        else
                        {
                            winStadium.StadiumDuskPositions[index].X = int.Parse(GetAttribute(pos, "x", "0"));
                            winStadium.StadiumDuskPositions[index].Y = int.Parse(GetAttribute(pos, "y", "0"));
                        }
                    }
                }
                if (winStadium.StadiumDuskPositions[0].X == 0)
                {
                    winStadium.StadiumDuskPositions = winStadium.StadiumNightPositions;
                }
                if (File.Exists(path + "\\" + stadium.Name + "_dusk.jpg") == false)
                {
                    winStadium.StadiumDuskPositions = winStadium.StadiumDayPositions;
                }
                if (File.Exists(path + "\\" + stadium.Name + "_night.jpg") == false)
                {
                    winStadium.StadiumNightPositions = winStadium.StadiumDuskPositions;
                }
            }
            finally
            {
                stadiumConfigDoc = null;
            }

            return winStadium;
        }
    }
}
